package cmu.mobile.lab;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FriendsList extends Activity{
	Intent result;
	Button love;
	Button eh;
	Button x;
	LinearLayout responded;
	LinearLayout notResponded;
	ImageView theIcon;
	String itemPhoneNumber;
	int itemID;
	String itemPath;
	MMSContent item;
	String itemStore;
	String itemSize;
	String itemPrice;
	String itemFriendName;
	String itemComment;
	Button rightArrow;
	Button leftArrow;
	String itemRating;
	TextView itemInfo;
	TextView advicePerson;
	TextView friendComment;
	ImageView friendItem;
	ArrayList<MMSContent> friends;
	int i;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_friendlist);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		result = new Intent();
		love = (Button)findViewById(R.id.loveIcon);
		eh = (Button)findViewById(R.id.ehIcon);
		x = (Button)findViewById(R.id.xIcon);
		responded = (LinearLayout)findViewById(R.id.loveBox);
		notResponded = (LinearLayout)findViewById(R.id.loveBox2);
		theIcon = (ImageView)findViewById(R.id.displayedIcon);
		itemInfo = (TextView)findViewById(R.id.store_info);
		advicePerson = (TextView)findViewById(R.id.name_entry);
		friendComment = (TextView)findViewById(R.id.my_message);
		friendItem = (ImageView)findViewById(R.id.item_pic);
		
		/*
		 * if they rated a friend's item, then the rating (response)
		 * should be displayed
		 */
		Database mDbHelper = new Database(FriendsList.this);
		// TODO: add to Database
		mDbHelper.open();
		friends= mDbHelper.getAllFriendsItems();
		//ArrayList<MMSContent> rating = mDbHelper.getRating(3); 
		//ArrayList<MMSContent> all = mDbHelper.getAllMyItems();
		mDbHelper.close();
		
		if( !((friends.size()-1) == 0) )
		{
			populatePage(0);
		}
		
		findViewById(R.id.left_button).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(i > 0 )
				{
					i--;
					populatePage(i);
				}
			}
		});
		
		findViewById(R.id.right_button).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(i < (friends.size()-1) )
				{
					i++;
					populatePage(i);
				}
			}
		});
		
		//onclick, SMS.sendSMS()
		findViewById(R.id.loveIcon).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				responded.setVisibility(View.VISIBLE);
				notResponded.setVisibility(View.INVISIBLE);
				theIcon.setImageResource(R.drawable.lovebutton);
				
				Database mDbHelper = new Database(FriendsList.this);
				mDbHelper.open();
				item.setRating(MMSContent.GREAT);
				mDbHelper.rateItem(itemID, item);
				mDbHelper.close();
				
				itemID = item.getItemID();
				itemPhoneNumber = item.getOwnerID();
				SMS.sendSMS(itemPhoneNumber, itemID, item);
			}
		});
		
		findViewById(R.id.ehIcon).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				responded.setVisibility(View.VISIBLE);
				notResponded.setVisibility(View.INVISIBLE);
				theIcon.setImageResource(R.drawable.ehbutton);
				
				Database mDbHelper = new Database(FriendsList.this);
				mDbHelper.open();
				item.setRating(MMSContent.SOSO);
				mDbHelper.rateItem(itemID, item);
				mDbHelper.close();
				
				itemID = item.getItemID();
				itemPhoneNumber = item.getOwnerID();
				SMS.sendSMS(itemPhoneNumber, itemID, item);
				
			}
		});
		findViewById(R.id.xIcon).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				responded.setVisibility(View.VISIBLE);
				notResponded.setVisibility(View.INVISIBLE);
				theIcon.setImageResource(R.drawable.xbutton);
				
				Database mDbHelper = new Database(FriendsList.this);
				mDbHelper.open();
				item.setRating(MMSContent.NOWAY);
				mDbHelper.rateItem(itemID, item);
				mDbHelper.close();
				
				itemID = item.getItemID();
				itemPhoneNumber = item.getOwnerID();
			}
		});
		
		// back button
		findViewById(R.id.BackButton).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				setResult(Activity.RESULT_CANCELED);
				finish();
			}
		});
		
    }
    
    public void populatePage(int i){
    	item = friends.get(i);			
		itemRating = item.getRating();
		itemFriendName = item.getFriend();
		itemComment = item.getItemComment();
		itemPath = item.getImagePath();
		itemSize = item.getSize();
		itemPrice = item.getPrice();
		itemStore = item.getStore();
		itemID = item.getItemID();
			
		
		if( itemSize == null )
			itemSize = "N/A";
		
		if( itemPrice == null )
			itemPrice = "N/A";
		
		if( itemStore == null )
			itemStore = "N/A";
		itemInfo.setText("Store: " + itemStore + " | Price: " + itemPrice + " | Size: " + itemSize);
		
		if (itemFriendName != null)
			advicePerson.setText(itemFriendName + " asked you for advice on...");
		else
			advicePerson.setText("Your friend asked you for advice on...");
		
		if (itemComment != null)
			friendComment.setText(itemComment);
		else
			friendComment.setText("No Comment Available.");
		
		if(itemPath != null)
		{
			Uri u = Uri.parse("content://media" + itemPath);
			friendItem.setImageURI(u);
		}
		else
			friendItem.setImageResource(R.drawable.girl);
		
		if(itemRating != null)
		{
			//display rating
			responded.setVisibility(View.VISIBLE);
			notResponded.setVisibility(View.INVISIBLE);
			if(itemRating.equals(MMSContent.GREAT))
			{
				theIcon.setImageResource(R.drawable.lovebutton);
			}
			else if(itemRating.equals(MMSContent.SOSO))
			{
				theIcon.setImageResource(R.drawable.ehbutton);
			}
			else
			{
				theIcon.setImageResource(R.drawable.xbutton);
			}
		}
		else
		{
			//ask them to respond
			responded.setVisibility(View.INVISIBLE);
			notResponded.setVisibility(View.VISIBLE);

			//you can send sms on click
		}
    }
    

	
	
	
}
